clear

set memory 100m

insheet using "JudgeDirectory.txt"

//Drop unnecessary data
drop in 1/186
drop if v1[_n+1] == "-#"
drop if v1 == "-#"

//Create an index to be able to play with sorting
generate index = 1 in 1
replace index = index[_n-1] + 1 if index == .

//Identify Judges' names
generate newid = 1 if regexm(v1[_n+1], ".*(Present Judicial Duties).*")
replace newid = 1 if regexm(v1[_n+1], ".*(Current judicial duties).*")

replace newid = 1 if newid[_n+1] == 1 & regexm(v1[_n+1], ".*(Date of birth:).*") // Correcting for formating
replace newid = . if regexm(v1, ".*(Date of birth:).*") // Correcting for formating
generate dutieserror = 1 if newid == 1 & regexm(v1, ".*(Judge, C).*")
replace dutieserror = 1 if newid == 1 & regexm(v1, ".*(Miscellaneous Remedies Division).*") //This will be used later (when getting judicial duties) to correct an error
replace dutieserror = 0 if dutieserror != 1
replace newid = . if regexm(v1, ".*(Judge, C).*") | regexm(v1, "Miscellaneous Remedies Division") // Correcting the error
replace newid = 1 if newid[_n+2] == 1 & v1[_n+2] == "Illinois Supreme Court in 2010" // Correcting for formating
replace newid = . if newid == 1 & v1 == "Illinois Supreme Court in 2010" // Correcting for formating

replace newid = 0 if newid == .

//Identify every line with the Judge it belongs to.
generate tempnames = v1 if newid == 1
replace tempnames = tempnames[_n-1] if newid == 0

//Create DOB variable
generate dob = v1 if regexm(v1, ".*(Date of birth).*")
replace dob = dob[_n-1] if dob == "" && tempnames == tempnames[_n-1] 
replace dob = dob[_n+1] if dob == "" && tempnames == tempnames[_n+1]
replace dob = substr(dob, strpos(dob, ": ") + 2, .)

compress dob

//Find the judge's subcircuit
generate subcircuit = v1 if regexm(v1, ".*(Subcircuit).*")
replace subcircuit = v1[_n-1] + " " + subcircuit if regexm(subcircuit, "^Subcircuit")
replace subcircuit = subcircuit[_n-1] if subcircuit == "" && tempnames == tempnames[_n-1]
gsort -index
replace subcircuit = subcircuit[_n-1] if subcircuit == "" && tempnames == tempnames[_n-1]
sort index
format %20s subcircuit

replace subcircuit = substr(subcircuit, 1, strpos(subcircuit, ":") -1)

//Get year of admittance to practice
generate yrbarred = v1 if regexm(v1, ".*(admitted).*") | regexm(v1, ".*(license).*")
replace yrbarred = yrbarred + " " + v1[_n+1] if yrbarred != ""
replace yrbarred = v1[_n-1] + " " + v1 if yrbarred != "" & !regexm(yrbarred, ".*[0-9].*")
replace yrbarred = "" if yrbarred != "" & !regexm(yrbarred, ".*(19).*")


replace yrbarred = "" if yrbarred == "license hearings for the Illinois Secretary of State. Since 1990, he has also been a member of the"
replace yrbarred = v1[_n-1] + " " + v1 if yrbarred == "admitted to the bar later that year. Mr. Tailor has been a Cook County Stateís Attorney since 1996 and"

generate tempyrbarred = substr(yrbarred, strpos(yrbarred, "19"),.)
generate temp = substr(tempyrbarred, 1, 4)
replace tempyrbarred = substr(tempyrbarred, 5, .)

replace tempyrbarred = substr(tempyrbarred, strpos(tempyrbarred, "admitted"), .)
replace temp = substr(tempyrbarred, strpos(tempyrbarred, "19"), .) if regexm(tempyrbarred, ",*19.*")
replace temp = substr(temp, 1, 4) if regexm(tempyrbarred, ".*19.*")

replace yrbarred = temp

drop tempyrbarred temp
compress yrbarred

replace yrbarred = yrbarred[_n-1] if yrbarred == "" & tempnames == tempnames[_n-1]
gsort -index
replace yrbarred = yrbarred[_n-1] if yrbarred == "" & tempnames == tempnames[_n-1]
sort index

//Get Present Judicial Duties
generate currentcourt = v1 if regexm(v1[_n-1], "(.*Present Judicial Duties).*") & dutieserror != 1
replace currentcourt = v1 if regexm(v1[_n-1], "(.*Current judicial duties).*")
replace currentcourt = currentcourt + " " + v1[_n+1] if currentcourt != "" & !regexm(v1[_n+1], ".*(Previous).*") & !regexm(v1[_n+1], ".*(Appointed).*") & !regexm(v1[_n+1], ".*(Elected).*") & !regexm(v1[_n+1], ".*(Recalled).*")
replace  currentcourt = "" if regexm(currentcourt, "Previous")

replace currentcourt = "Judge, Circuit Court, Child" + " " + currentcourt in 276

replace currentcourt = currentcourt[_n-1] if currentcourt == "" & tempnames == tempnames[_n-1]
gsort -index
replace currentcourt = currentcourt[_n-1] if currentcourt == "" & tempnames == tempnames[_n-1]
sort index

format %20s currentcourt

//Get Previous Judicial Duties
generate temp = .
generate prev_judexp = v1 if regexm(v1, "(.*Previous Judicial Duties).*") | dutieserror[_n-2] == 1
replace temp = 0 if prev_judexp != ""
replace temp = 1 if regexm(v1, "^(Evaluation).*") | regexm(v1, "^(Appointed).*") | regexm(v1, "^(Elected).*") | regexm(v1, "^(Recalled).*")
replace temp = temp[_n-1] if tempnames == tempnames[_n-1] & temp == .
replace prev_judexp = prev_judexp + " " + v1[_n+1] if prev_judexp != "" & temp[_n+1] == 0 & tempnames == tempnames[_n+1]
replace prev_judexp = prev_judexp + " " + v1[_n+2] if prev_judexp != "" & temp[_n+2] == 0 & tempnames == tempnames[_n+2]
replace prev_judexp = prev_judexp + " " + v1[_n+3] if prev_judexp != "" & temp[_n+3] == 0 & tempnames == tempnames[_n+3]
replace prev_judexp = prev_judexp + " " + v1[_n+4] if prev_judexp != "" & temp[_n+4] == 0 & tempnames == tempnames[_n+4]
gen prev_judexpB = v1[_n+5] if prev_judexp != "" & temp[_n+5] == 0 & tempnames == tempnames[_n+5]
replace prev_judexpB = prev_judexpB + " " + v1[_n+6] if prev_judexp != "" & temp[_n+6] == 0 & tempnames == tempnames[_n+6]
replace prev_judexpB = prev_judexpB + " " + v1[_n+7] if prev_judexp != "" & temp[_n+7] == 0 & tempnames == tempnames[_n+7]
replace prev_judexpB = prev_judexpB + " " + v1[_n+8] if prev_judexp != "" & temp[_n+8] == 0 & tempnames == tempnames[_n+8]
replace prev_judexpB = prev_judexpB + " " + v1[_n+9] if prev_judexp != "" & temp[_n+9] == 0 & tempnames == tempnames[_n+9]
replace prev_judexp = substr(prev_judexp, strpos(prev_judexp, "Previous Judicial Duties") + 25, .) if regexm(prev_judexp, "Previous Judicial Duties")
replace prev_judexp = strtrim(prev_judexp)

replace prev_judexp = prev_judexp[_n-1] if prev_judexp == "" & tempnames == tempnames[_n-1]
replace prev_judexpB = prev_judexpB[_n-1] if prev_judexpB == "" & tempnames == tempnames[_n-1]
gsort -index
replace prev_judexp = prev_judexp[_n-1] if prev_judexp == "" & tempnames == tempnames[_n-1]
replace prev_judexpB = prev_judexpB[_n-1] if prev_judexpB == "" & tempnames == tempnames[_n-1]
sort index

format %20s prev_judexp
format %20s prev_judexpB
drop temp

//Modes of election
generate temp = .
generate mode = ""
replace mode = v1 if regexm(v1, "^(Appointed).*") | regexm(v1, "^(Elected).*")
replace temp = 0 if mode != ""
replace temp = 1 if regexm(v1, "^(Evaluation).*") | regexm(v1, "^(Previous).*")
replace temp = temp[_n-1] if tempnames == tempnames[_n-1] & temp == .
replace mode = mode + " " + v1[_n+1] if mode != "" & temp[_n+1] == 0 & tempnames == tempnames[_n+1]
replace mode = mode + " " + v1[_n+2] if mode != "" & temp[_n+2] == 0 & tempnames == tempnames[_n+2]
replace mode = mode + " " + v1[_n+3] if mode != "" & temp[_n+3] == 0 & tempnames == tempnames[_n+3]

replace mode = substr(mode, 1, strpos(mode, "Present Judicial")-1) if regexm(mode, "Present Judicial") //Fixing a formatting error
replace mode = subinstr(mode, "by the Illinois Supreme Court ", " " , 1) if regexm(mode, " Appointed to the Illinois Supreme Court by the Illinois Supreme Court")
replace mode = mode[_n+10] + "; " + mode if mode != "" & mode[_n+10] != "" & tempnames == tempnames[_n+10]

replace mode = mode[_n-1] if mode == "" & tempnames == tempnames[_n-1]
gsort -index
replace mode = mode[_n-1] if mode == "" & tempnames == tempnames[_n-1]
sort index

format %20s mode
drop temp

//yr degree
generate yrdegree = v1[_n-1] + " " + v1 + " " + v1[_n+1] if regexm(v1, "University") | regexm(v1, "Law School") | regexm(v1, "degree") 
replace yrdegree = "" if !regexm(yrdegree, "19[0-9][0-9].*")
replace yrdegree = "" if regexm(yrdegree, "professor")
replace yrdegree = "" if regexm(yrdegree, "Professor")

generate temp = 1 in 287
replace temp = 1 in 2625
replace temp = 1 in 2627
replace temp = 1 in 3695
replace yrdegree = "" if temp == 1
drop temp

replace yrdegree = substr(yrdegree, strpos(yrdegree, " 19"), .)
replace yrdegree = substr(yrdegree, strpos(yrdegree, " 1984"), .) in 3977

replace yrdegree = substr(yrdegree, 2, 4)
compress yrdegree

replace yrdegree = yrdegree[_n-1] if yrdegree == "" & tempnames == tempnames[_n-1]
gsort -index
replace yrdegree = yrdegree[_n-1] if yrdegree == "" & tempnames == tempnames[_n-1]
sort index

//Evaluations
gen temp = v1 if regexm(v1, "Qualified")
replace temp = v1[_n-1] + " " + temp if regexm(temp, "^Qualified$")
replace temp = v1[_n-1] + " " + temp if regexm(temp, "^Well Qualified$")
replace temp = v1[_n-1] + " " + temp if regexm(temp, "^Not Qualified$")
replace temp = v1[_n-1] + " " + temp if regexm(temp, "^Highly Qualified$")
replace temp = "" if regexm(temp, "finds")
replace temp = "" if regexm(temp, "found")
replace temp = "" if regexm(temp, "^h")
replace temp = "" if regexm(temp, "Qualified\.$")
replace temp = "" if temp == "Qualified for retention."
replace temp = "" if regexm(temp, "\.$")
replace temp = "" if !regexm(temp, ":") & !regexm(temp, "[0-9]") & !regexm(temp, "Evaluation")
replace temp = "" if regexm(temp, "find")
replace temp = v1[_n-1] + " " + temp if regexm(temp, "^Court")
replace temp = v1[_n-1] + " " + temp if regexm(temp, "^Bench")
replace temp = v1[_n-1] + " " + temp if regexm(temp, "^\(")
replace temp = v1[_n-1] + " " + temp if temp != "" & !regexm(temp, "[0-9]") & !regexm(temp, "^Eval") & v1[_n-1] != "Evaluation" & !regexm(v1[_n-1], ":")

replace temp = "; " + temp if temp != ""
replace temp = temp[_n-1] + temp if tempnames == tempnames[_n-1]
replace temp = substr(temp, 3, .)
gsort -index
replace temp = temp[_n-1] if tempnames == tempnames[_n-1]
sort index

rename temp eval
format %20s eval

//Keep only one observation for each judge
drop if newid == 0

drop newid
drop dutieserror
drop index
drop v1

gen index = 1 in 1
replace index = index[_n-1] + 1 if index == .

//Separate yob from dob variables
gen temp = dob
replace temp = substr(temp, strpos(temp, "/")+4, .) if regexm(temp, "/")
gen yob = real(temp)
drop temp
replace yob = yob + 1900 if yob < 1800

replace dob = substr(dob, 1, strpos(dob, "/")+2) if regexm(dob, "/")
replace dob = "" if !regexm(dob, "/")

//Separate First from Last names
gen lastname = regexs(1) if regexm(tempnames, "([A-Z][A-Z]+)")
replace lastname = regexs(1) if regexm(tempnames, "([A-Z]+ [A-Z]+)")
replace lastname = regexs(1) if regexm(tempnames, "([A-Z]+ [A-Z]+ [A-Z]+)")
replace lastname = regexs(1) if regexm(tempnames, "(Mc[A-Z]+)")
replace lastname = regexs(2) if regexm(tempnames, "(Mc[A-Z][a-z]+) ([A-Z]+)")
replace lastname = regexs(1) if regexm(tempnames, "([a-zA-Z]+\-[A-Z]+)")
replace lastname = regexs(3) if regexm(tempnames, "(^[A-Z][a-z]+) ([A-Z]\.) ([A-Z][a-z]+).*") & lastname == ""
replace lastname = regexs(2) if regexm(tempnames, "(^[A-Z][a-z]+) ([a-zA-Z]+)") & lastname == ""

gen firstname = regexs(1) if regexm(tempnames, "(^[A-Z][a-z]+ [A-Z]\.) [A-Z]")
replace firstname = regexs(1) if regexm(tempnames, "(^[A-Z][a-z]+ [A-Z][a-z]+) [A-Z]")
replace firstname = regexs(1) if regexm(tempnames, "(^[A-z][a-z]+ [A-Z]\. [A-Z][a-z]+) [A-Z]+")
replace firstname = regexs(1) if regexm(tempnames, "(^[A-Z][a-z]+ Mc[A-Z][a-z]+) ([A-Z]+)")
replace firstname = regexs(1) if regexm(tempnames, "(^[A-Z][a-z]+) [A-Z]") & firstname == ""
replace firstname = regexs(1) if regexm(tempnames, "^([A-Z]\. [A-Z][a-z]+).*") & firstname == ""
replace firstname = regexs(1) if regexm(tempnames, "^([a-zA-Z]+ [A-Z]\.) [A-Z]") & firstname == ""
replace firstname = regexs(1) if regexm(tempnames, "^([a-zA-Z]+) [A-Z]") & firstname == ""

replace lastname = strupper(lastname)

//Separate Designation, District and Division from currenctcourt
replace currentcourt = "Judge, Circuit Court, Domestic Relations Division 2010 Primary and General Elections, Circuit Court" if currentcourt == "Judge, Circuit Court, Domestic Relations Division 2010 Primary and General Elections, Circuit Court:"
replace currentcourt = "Judge, Circuit Court, First Municipal District" if currentcourt == "Judge, Circuit Court, First Municipal District Evaluation: Highly Qualified (Judge Gubin was"
gen designation = substr(currentcourt, strpos(currentcourt, ":") + 1, .) if regexm(currentcourt, ",")
replace designation = substr(designation, 1, strpos(designation, ",")-1)
replace designation = currentcourt if !regexm(currentcourt, ",")
replace designation = substr(designation, strpos(designation, "Present") + 9, .) if regexm(designation, "Present") 
replace designation = strtrim(designation)
compress designation

replace designation = "Judge" if designation == "udge"

replace currentcourt = "" if !regexm(currentcourt, ",")
replace currentcourt = substr(currentcourt, strpos(currentcourt, ":") + 1, .)
replace currentcourt = strtrim(currentcourt)

gen temp = substr(currentcourt, strpos(currentcourt, ",") + 1, .)

replace currentcourt = substr(temp, 1, strpos(temp, ",") - 1)
replace currentcourt = temp if !regexm(temp, ",") & regexm(temp, "Court")
replace currentcourt = strtrim(currentcourt)
compress currentcourt

replace temp = substr(temp, strpos(temp, ",") +1, .)
replace temp = strtrim(temp)
replace temp = "" if temp == "Illinois Supreme Court"
replace temp = "Criminal Division" if temp == "Criminal Division, F"

gen temp2 = substr(temp, strpos(temp, ",") + 1, .) if regexm(temp, ",")
replace temp = substr(temp, 1, strpos(temp, ",") - 1) if regexm(temp, ",")

gen district = temp if regexm(temp, "District")
replace district = temp2 if regexm(temp2, "District")

gen division = temp if regexm(temp, "Division")
replace division = temp2 if regexm(temp2, "Division")
replace division = temp if temp == "Domestic Relations"
replace division = temp if temp == "Domestic Violence"
replace division = "Chancery Division" if temp == "Chancery"

gen section = temp if regexm(temp, "Section")
replace section = temp2 if regexm(temp2, "Section")
replace section = temp2 if temp2 == " Mortgage Foreclosure and Mechanic's Lien"

replace section = "Tax & Miscellaneous Remedies Section" if division == " Tax & Miscellaneous Remedies Division"
replace division = "Law Division" if division == " Tax & Miscellaneous Remedies Division"

drop temp
drop temp2

format %27s district
format %20s division
format %20s section

//Reformat
rename index id
keep id lastname firstname dob yob yrdegree yrbarred designation currentcourt district division section subcircuit mode eval prev_judexp prev_judexpB
order id lastname firstname dob yob yrdegree yrbarred designation currentcourt district division section subcircuit mode eval prev_judexp prev_judexpB

replace lastname = "O'BRIEN" if lastname == "BRIEN"
replace lastname = "O'DONNELL" if lastname == "DONNELL"
replace lastname = "O'GARA" if lastname == "GARA"
replace lastname = "O'HARA" if lastname == "HARA"
replace lastname = "O'NEAL" if lastname == "NEAL"
replace lastname = "O'NEILL BURKE" if lastname == "NEILL BURKE"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Prepare some names to merge dataset
replace mode = mode + "; " + mode[_n+1] if id == 157 //Correcting for a duplicate entry in the original directory
replace prev_judexp = prev_judexp[_n+1] if id == 157 //Correcting for a duplicate entry in the original directory
drop if id == 158 //Correcting for a duplicate entry in the original directory
drop id //This variable was useless anyway

////Names that are written diffrent in both databases
replace firstname = "LaQuietta" if firstname == "La Quietta"
replace lastname = "CLAY-HERRON" if lastname == "CLAY-CLARK"
replace lastname = "DELEHANTY" if lastname == "DELAHANTY"
replace lastname = "EDIDIN" if lastname == "EDIDEN"
replace lastname = "GOMOLINSKI" if lastname == "GOMOLINKSI"
replace firstname = "Michael" if firstname == "Mike"
replace lastname = "SCHLEIFER" if lastname == "SCHLIEFER"
replace lastname = "WARD KIRBY" if lastname == "WARD-KIRBY"

//Format variables so that they can be combined variables
gen yrdegree_n = real(yrdegree)
drop yrdegree
rename yrdegree_n yrdegree

//Merge
gen temp = regexs(1) if regexm(firstname, "([a-zA-Z]+) .*")
replace temp = firstname if temp == ""
gen names = temp + " " + lastname
drop temp

rename subcircuit subc

/*Here merges in the biographical information collected Chicago Council of Lawyers
The organizaiton collected personal characteristics, professional background, and judicial career information for judges and shared them so that they could be used for this project.*/


*Saving the final Chicago Council of Lawyers judge biography dataset





